//
//  GangarScene.swift
//  aircraftBattle1
//
//  Created by 许洋 on 2024/5/16.
//

import SpriteKit
import AVFoundation

class GangarScene:SKScene {
    private var bgNode:SKSpriteNode!
    private var aircraftNode1:SKSpriteNode!
    private var aircraftNode2:SKSpriteNode!
    private var aircraftNode3:SKSpriteNode!
    private var confirmNode:SKSpriteNode!
    private var selectIndex:NSInteger!

    
    override func didMove(to view: SKView) {
        
        bgNode = childNode(withName: "bg") as? SKSpriteNode
        bgNode.size = self.size
        aircraftNode1 = childNode(withName: "Aircraft1") as? SKSpriteNode
        aircraftNode2 = childNode(withName: "Aircraft2") as? SKSpriteNode
        aircraftNode3 = childNode(withName: "Aircraft3") as? SKSpriteNode
        confirmNode = childNode(withName: "Confirm") as? SKSpriteNode
        if !UserDefaults.standard.bool(forKey: "GANGAR") {
            selectIndex = UserDefaults.standard.object(forKey: "GANGAR") as? NSInteger
        }
        updateAircraftState()

    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard  let touch = touches.first else {
            return
        }
        let touchLocation = touch.location(in: self)
        if  aircraftNode1.contains(touchLocation) {
            selectIndex = 0
            
            if !UserDefaults.standard.bool(forKey: "GANGAR") {
                UserDefaults.standard.set(0, forKey: "GANGAR")
            }
        }
        if  aircraftNode2.contains(touchLocation) {
            selectIndex = 1

            if !UserDefaults.standard.bool(forKey: "GANGAR") {
                UserDefaults.standard.set(1, forKey: "GANGAR")
            }
        }
        if  aircraftNode3.contains(touchLocation) {
            selectIndex = 2

            if !UserDefaults.standard.bool(forKey: "GANGAR") {
                UserDefaults.standard.set(2, forKey: "GANGAR")
            }

        }
        updateAircraftState()
        
        if  confirmNode.contains(touchLocation) {
            if !UserDefaults.standard.bool(forKey: "GANGAR") {
                UserDefaults.standard.set(selectIndex, forKey: "GANGAR")
            }
            let reveal = SKTransition.doorsCloseHorizontal(withDuration: TimeInterval(0.5))
            let mainScene = HomeScene(fileNamed: "HomeScene")
            mainScene?.size = self.size
            mainScene?.scaleMode = .aspectFill
            self.view?.presentScene(mainScene!, transition: reveal)
            
        }


        
    }
    func updateAircraftState() {
        switch selectIndex{
        case 0:
            aircraftNode1.texture = SKTexture(imageNamed: "")
            aircraftNode2.texture = SKTexture(imageNamed: "")
            aircraftNode3.texture = SKTexture(imageNamed: "")

            break
        case 1:
            aircraftNode1.texture = SKTexture(imageNamed: "")
            aircraftNode2.texture = SKTexture(imageNamed: "")
            aircraftNode3.texture = SKTexture(imageNamed: "")
            break
        case 2:
            aircraftNode1.texture = SKTexture(imageNamed: "")
            aircraftNode2.texture = SKTexture(imageNamed: "")
            aircraftNode3.texture = SKTexture(imageNamed: "")
            break
        default:
            break
        }

    }
}
